package com.grace.web.controller.cms;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.oly.cms.admin.service.ICmsTagService;
import com.oly.cms.common.domain.entity.CmsTag;
import com.oly.cms.common.enums.CommonVisibleEnums;
import com.grace.common.annotation.Log;
import com.grace.common.core.controller.BaseController;
import com.grace.common.core.domain.AjaxResult;
import com.grace.common.enums.BusinessType;
import com.grace.common.utils.poi.ExcelUtil;
import com.grace.common.core.page.TableDataInfo;

/**
 * 内容标签Controller
 * 
 * @author zhige
 * @date 2022-11-17
 */
@RestController
@RequestMapping("/cms/tag")
public class CmsTagController extends BaseController {
    @Autowired
    private ICmsTagService cmsTagService;

    /**
     * 查询内容标签列表
     */
    @PreAuthorize("@ss.hasPermi('cms:tag:list')")
    @GetMapping("/list")
    public TableDataInfo list(CmsTag cmsTag) {
        startPage();
        List<CmsTag> list = cmsTagService.listCmsTagByTag(cmsTag);
        return getDataTable(list);
    }

    /**
     * 导出内容标签列表
     */
    @PreAuthorize("@ss.hasPermi('cms:tag:export')")
    @Log(title = "内容标签", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CmsTag cmsTag) {
        List<CmsTag> list = cmsTagService.listCmsTagByTag(cmsTag);
        ExcelUtil<CmsTag> util = new ExcelUtil<CmsTag>(CmsTag.class);
        util.exportExcel(response, list, "内容标签数据");
    }

    /**
     * 获取内容标签详细信息
     */
    @PreAuthorize("@ss.hasPermi('cms:tag:query')")
    @GetMapping(value = "/{tagId}")
    public AjaxResult getInfo(@PathVariable("tagId") Long tagId) {
        return success(cmsTagService.selectCmsTagById(tagId));
    }

    /**
     * 新增内容标签
     */
    @PreAuthorize("@ss.hasPermi('cms:tag:add')")
    @Log(title = "内容标签", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody CmsTag cmsTag) {
        cmsTag.setCreateBy(getUsername());
        return toAjax(cmsTagService.insertCmsTag(cmsTag));
    }

    /**
     * 修改内容标签
     */
    @PreAuthorize("@ss.hasPermi('cms:tag:edit')")
    @Log(title = "内容标签", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody CmsTag cmsTag) {
        cmsTag.setUpdateBy(getUsername());
        return toAjax(cmsTagService.updateCmsTagById(cmsTag));
    }

    /**
     * 批量更新标签状态
     */
    @PreAuthorize("@ss.hasPermi('cms:tag:edit')")
    @Log(title = "内容标签", businessType = BusinessType.UPDATE)
    @PutMapping("/batchVisible/{tagIds}/{visibleEnum}")
    public AjaxResult batchVisible(@PathVariable Long[] tagIds, @PathVariable CommonVisibleEnums visibleEnum) {
        return toAjax(cmsTagService.batchVisible(tagIds, visibleEnum, getUsername()));
    }

    /**
     * 批量更新标签排序
     */
    @PreAuthorize("@ss.hasPermi('cms:tag:edit')")
    @Log(title = "内容标签", businessType = BusinessType.UPDATE)
    @PutMapping("/batchOrderNum/{tagIds}/{orderNum}")
    public AjaxResult batchOrderNum(@PathVariable Long[] tagIds, @PathVariable long orderNum) {
        return toAjax(cmsTagService.batchOrderNum(tagIds, orderNum, getUsername()));
    }

    /**
     * 批量删除内容标签
     */
    @PreAuthorize("@ss.hasPermi('cms:tag:remove')")
    @Log(title = "内容标签", businessType = BusinessType.DELETE)
    @DeleteMapping("/{tagIds}")
    public AjaxResult batchRemove(@PathVariable Long[] tagIds) {
        return toAjax(cmsTagService.deleteCmsTagByIds(tagIds));
    }
}
